package cn.openbiotoken.impl;

import cn.openbiotoken.AgeEstimation;
import cn.openbiotoken.Biometrics;

import static cn.openbiotoken.Biometrics.DEFAULT_CONFIDENCE_PERCENTAGE;
import static cn.openbiotoken.Biometrics.MAX_CONFIDENCE_PERCENTAGE;
import static cn.openbiotoken.Biometrics.MIN_CONFIDENCE_PERCENTAGE;
import static java.lang.String.format;

public class AgeEstimationImpl implements AgeEstimation {

    public AgeEstimationImpl(final byte age, final byte confidencePercentage) {
        super();

        if (age < MIN_AGE) {
            throw new IllegalArgumentException(format("The age should be greater than or equal to %d. Actual age is %d.", MIN_AGE, age));
        }

        if (confidencePercentage > MAX_CONFIDENCE_PERCENTAGE) {
            throw new IllegalArgumentException(format("The confidence percentage should be less than or equal to %d. Actual confidence percentage is %d.", MAX_CONFIDENCE_PERCENTAGE, confidencePercentage));
        }
        if (confidencePercentage < MIN_CONFIDENCE_PERCENTAGE) {
            throw new IllegalArgumentException(format("The confidence percentage should be greater than or equal to %d. Actual confidence percentage is %d.", MIN_CONFIDENCE_PERCENTAGE, confidencePercentage));
        }

        this.age = age;
        this.confidencePercentage = confidencePercentage;
    }

    public AgeEstimationImpl(final int age, final int confidencePercentage) {
        this(AgeEstimation.legalizeAge(age), Biometrics.legalizeConfidencePercentage(confidencePercentage));
    }

    public AgeEstimationImpl(final int age) {
        this(age, DEFAULT_CONFIDENCE_PERCENTAGE);
    }

    private final byte age;

    private final byte confidencePercentage;

    @Override
    public byte getAge() {
        return age;
    }

    @Override
    public byte getConfidencePercentage() {
        return confidencePercentage;
    }
}
